package io.renren.modules.sys.back.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.common.utils.Constant;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.back.entity.BtCategroyManageEntity;
import io.renren.modules.sys.back.service.BtCategroyManageService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author goudandan
 * @email resjz1@163.com
 * @date 2018-09-13 15:01:50
 */
@RestController
@RequestMapping("sys/btcategroymanage")
public class BtCategroyManageController extends AbstractController{
    @Autowired
    private BtCategroyManageService btCategroyManageService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:btcategroymanage:list")
    public List<BtCategroyManageEntity> list(@RequestParam Map<String, Object> params){
        List<BtCategroyManageEntity> deptList = btCategroyManageService.queryList(params);

        return deptList;
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{deptId}")
    @RequiresPermissions("sys:btcategroymanage:info")
    public R info(@PathVariable("deptId") Long deptId){
        BtCategroyManageEntity dept = btCategroyManageService.selectById(deptId);

        return R.ok().put("dept", dept);
    }

    /**
     * 信息
     */
    /**
     * 上级部门Id(管理员则为0)
     */
    @RequestMapping("/info")
    @RequiresPermissions("sys:btcategroymanage:list")
    public R info(){
        long deptId = 0;
        if(getUserId() != Constant.SUPER_ADMIN){
            List<BtCategroyManageEntity> deptList = btCategroyManageService.queryList(new HashMap<String, Object>());
            Long parentId = null;
            for(BtCategroyManageEntity sysDeptEntity : deptList){
                if(parentId == null){
                    parentId = sysDeptEntity.getPid();
                    continue;
                }

                if(parentId > sysDeptEntity.getPid().longValue()){
                    parentId = sysDeptEntity.getPid();
                }
            }
            deptId = parentId;
        }

        return R.ok().put("id", deptId);
    }
    /**
            * 选择部门(添加、修改菜单)
	 */
    @RequestMapping("/select")
    @RequiresPermissions("sys:btcategroymanage:list")
    public R select(){
        List<BtCategroyManageEntity> deptList = btCategroyManageService.queryList(new HashMap<String, Object>());
        BtCategroyManageEntity old = new BtCategroyManageEntity();
        old.setName("分类选着");
        old.setId(0L);
        old.setPid((long)(-1));
        deptList.add(old);
        return R.ok().put("deptList", deptList);
    }
    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:btcategroymanage:save")
    public R save(@RequestBody BtCategroyManageEntity btCategroyManage){
        Long fid = btCategroyManage.getPid();
        //父类下的所有分类
        List<BtCategroyManageEntity> pbeanlist = btCategroyManageService.selectList(new EntityWrapper<BtCategroyManageEntity>().eq("pid",fid));
        if(pbeanlist.size()<=0){//没有分类
            BtCategroyManageEntity fbean = btCategroyManageService.selectById(fid);
            String str = fbean.getCatatype();
            btCategroyManage.setCatatype(str+","+1);
            btCategroyManage.setDeepnum(fbean.getDeepnum()+1);
        }else{
            Integer com = 0;
            Integer tdeep = 0;
            String str = "";
            String lstr = "";
            for(BtCategroyManageEntity sbe :pbeanlist){
                List<String> gameids = Arrays.asList(sbe.getCatatype().split(","));
                if(com.compareTo(Integer.parseInt(gameids.get(gameids.size()-1)))<=0){
                    com = Integer.parseInt(gameids.get(gameids.size()-1));
                    str =sbe.getCatatype();
                    tdeep = sbe.getDeepnum();
                }
            }
            if(str != ""){
                List<String> gameids = Arrays.asList(str.split(","));
                gameids.set(gameids.size()-1,String.valueOf(com+1));
                for(int i=0;i<gameids.size();++i){
                    // lstr=gameids.get(i)+",";
                    if(i+1==gameids.size()){
                        lstr+=gameids.get(i);
                    }else{
                        lstr +=gameids.get(i)+",";
                    }
                }
                btCategroyManage.setCatatype(lstr);
                btCategroyManage.setDeepnum(tdeep);
            }
        }

        btCategroyManageService.insert(btCategroyManage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:btcategroymanage:update")
    public R update(@RequestBody BtCategroyManageEntity btCategroyManage){
        ValidatorUtils.validateEntity(btCategroyManage);
        btCategroyManageService.updateAllColumnById(btCategroyManage);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:btcategroymanage:delete")
    public R delete(long deptId){
        //判断是否有子部门
        List<Long> deptList = btCategroyManageService.queryCateMagIdList(deptId);
        if(deptList.size() > 0){
            return R.error("请先删除子部门");
        }

        btCategroyManageService.deleteById(deptId);

        return R.ok();
    }

}
